#!/usr/bin/env python
"""Create a SciTE API file from wowwiki.com/API.  Output it and a suitably set-up SciteDirectory.properties.
Note that you must set properties.directory.enable=1 in your scite global properties for this to work.
I would like to acknowledge that this isn't done in the least bit elegantly.  But it works, so there's that.
"""

import re
import urllib2

htmlre = re.compile(r'<.+?>(.+?)</.+?>')
apire = re.compile(r"""
    ^                       # From the beginning of the line!
    </?d.+?d>               # html junk
    \s*([A-Z ]*?)\s*        # All-uppercase words before the link, if present
    <a.+?>(.+)</a>          # The text of the link
    (\(.*?\))               # The argument list following the link, including its parentheses
    \s*(?:&nbsp;)?\s*-?\s*  # Some whitespace and html junk that we don't care about (so a non-exported group)
    (.*?)                   # Comments about the function, if present
    #\n
    $                       # To the end of the line!
    """, re.VERBOSE)
# Tests for the regexp:
#print apire.match("""</dd><dd> <a href="/index.php?title=API_SetPortraitToTexture&amp;action=edit" class="new" title="API SetPortraitToTexture">SetPortraitToTexture</a>("texture", icon) &nbsp; - Paint a Texture object with the given Texture&nbsp;?""")
#print apire.match(""" <a href="/index.php?title=API_SetBindingSpell&amp;action=edit" class="new" title="API SetBindingSpell">SetBindingSpell</a>("KEY", "Spell Name") &nbsp; - Set a key binding directly to a spell, uses the same spell name syntax as /cast.""").groups()
#print apire.match(""" <a href="/API_GetAutoLootToggleKey" title="API GetAutoLootToggleKey">GetAutoLootToggleKey</a>() &nbsp; -""").groups()
#print apire.match(""" PROTECTED UI <a href="/API_ActionButtonDown" title="API ActionButtonDown">ActionButtonDown</a>(id) &nbsp; - Press the specified action button. (2.0 Protected)""").groups()

def fetch_page_as_list(url):
    request = urllib2.Request(url)
    request.add_header("User-agent", "apigen.py")
    page = urllib2.urlopen(request)
    p = page.readlines()
    page.close()
    return p

p = fetch_page_as_list("http://www.wowwiki.com/API") + fetch_page_as_list("http://wowwiki.com/Widget_API")
api = []
for l in p:
    m = apire.match(l)
    if m:
        g = m.groups()
        if g[1].find("REMOVED") == -1:
            api.append(g[1].strip()+' '+htmlre.sub(r'\1', g[2])+(len(g[0])>0 and (' '+g[0]) or '')+(len(g[3])>0 and ' - '+g[3] or '')+'\n')
apifile=open('lua_wow.api', 'w')
api.sort()
apifile.writelines(api)
apifile.close()

# Write a SciteDirectory.properties file:
propfile = open('SciteDirectory.properties', 'w')
propfile.write("""#api.$(file.patterns.lua)=$(api.$(file.patterns.lua));$(SciteDirectoryHome)/lua_wow.api
api.$(file.patterns.lua)=$(SciteDirectoryHome)/lua_wow.api
autocomplete.choose.single=1
autocomplete.lua.ignorecase=1
autocomplete.lua.start.characters=.:
autocomplete.lua.fillups=(
#autocompleteword.automatic
calltip.lua.ignorecase=1
""")
propfile.close()